#!/usr/bin/env python
# coding: utf-8

import tornado.web
import tornado.httpserver
import tornado.ioloop
from tornado.web import RequestHandler, url

'''
路由参数的获取
'''

class IndexHandler(RequestHandler):
    def get(self):
        self.write("index.html")

class CityHandler(RequestHandler):
    def get(self, city1, city2):
        '''
        http://me.localhost.cn:8000/city/gz/liwan
        :param city1: gz
        :param city2: liwan
        :return: "City: gz liwan"
        '''
        self.write("City: %s %s"%(city1, city2))

class LangHandler(RequestHandler):
    def get(self, *args, **kwargs):
        '''
        http://me.localhost.cn:8000/lang/python/2017
        :param args: ()
        :param kwargs: {'type': 'python', 'year': '2017'}
        :return: "Lang: python 2017"
        '''
        self.write("Lang: %s %s"%(kwargs['type'], kwargs['year']))

if __name__ == '__main__':
    app = tornado.web.Application([
        url(r'/', IndexHandler),
        url(r'/city/(\w+)/(\w+)', CityHandler),
        url(r'/lang/(?P<type>\w+)/(?P<year>\d{4})', LangHandler),
    ])
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.bind(8000)
    http_server.start(1)
    tornado.ioloop.IOLoop.current().start()